<template>
  <el-dialog
    :title="showTitle"
    :visible.sync="myAddDeptShow"
    width="60%"
    :before-close="handleClose"
  >
    <el-form ref="fromRef" :model="fromData" label-width="100px" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="fromData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="fromData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="fromData.manager" style="width:80%" placeholder="请选择" @focus="getEmployeeSimple">
          <el-option v-for="p in people" :key="p.id" :value="p.username" :label="p.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="fromData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <el-row slot="footer" class="dialog-footer" type="flex" justify="center">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="btnOK">确 定</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { addDepartmnetAPI, getDepartmentsAPI, getDepartmnetByidAPI, updataDepartmnetByidAPI } from '@/api/departments'
import { getEmployeeSimpleAPI } from '@/api/employee'
export default {
  name: 'AddDept',
  components: {},
  props: {
    myAddDeptShow: {
      required: true,
      type: Boolean
    },
    treeNode: { // 要添加的部门的上级
      default: null,
      type: Object
    }
  },
  data() {
    const checkNameRepeat = async(rule, value, callback) => {
      // 输入部门名称 value
      // 父部门 this.treeNode
      // 兄弟部门 this.treeNode.children
      const { depts } = await getDepartmentsAPI() // 请求所有部门
      let borthers
      if (this.fromData.id) {
        // 编辑
        borthers = depts.filter(t => t.pid === this.treeNode.id && t.id !== this.fromData.id)
      } else {
        // 新增
        borthers = depts.filter(t => t.pid === this.treeNode.id)
      }
      const f = borthers.some(t => t.name === value)
      f ? callback(new Error('出现重复部门')) : callback()
    }

    const checkCodeRepeat = async(rule, value, callback) => {
      const { depts } = await getDepartmentsAPI() // 请求所有部门
      let isRepeat
      if (this.fromData.id) {
        // 编辑
        isRepeat = depts.filter(t => t.id !== this.fromData.id).some(t => t.code === value)
      } else {
        // 新增
        isRepeat = depts.some(t => t.code === value && value)
      }

      isRepeat ? callback(new Error('出现重复id')) : callback()
    }
    return {
      fromData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: ''// 部门介绍

      },
      people: [], // 员工列表
      // 校验规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkNameRepeat }],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkCodeRepeat }
        ],
        manager: [
          { required: true, message: '请选择', trigger: 'blur' }],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showTitle() {
      return this.fromData.id ? '编辑本部' : '新增部门'
    }
  },
  watch: {},
  created() {},
  methods: {
    async getDepartmnetByid(id) {
      this.fromData = await getDepartmnetByidAPI(id)
    },
    // 确认按钮 添加部门/编辑部门  -----------------------------
    async btnOK() {
      try {
        // 验证表单
        await this.$refs.fromRef.validate()
        // 提交表单
        if (this.fromData.id) {
          await updataDepartmnetByidAPI(this.fromData)
        } else {
          await addDepartmnetAPI({
            ...this.fromData, pid: this.treeNode.id
          })
        }

        // 关闭弹层
        this.handleClose()
        // 通知父组件刷新
        this.$emit('addDepts')
        this.$message.success('ok')
      } catch (e) {
        console.log('失败', e)
      }
    },
    // 关闭弹层 ------------------------
    handleClose() {
      this.$refs.fromRef.resetFields()
      // 清空id
      // 方法一 this.fromData = this.$options.data().fromData
      if (this.fromData.id) this.fromData.id = '' // 方法二
      this.$emit('myoff')
    },
    // 获得员工列表 ---------------------
    async getEmployeeSimple() {
      this.people = await getEmployeeSimpleAPI()
    }
  }
}
</script>
<style lang="less" scoped />
